package com.lanren.familybookapp.ui.main.presenter;

import android.os.CountDownTimer;

import com.lanren.common.base.impl.BasePresenter;
import com.lanren.common.utils.LogUtils;
import com.lanren.familybookapp.ui.main.interfaces.IWelcomeModel;
import com.lanren.familybookapp.ui.main.model.WelcomeModel;

/**
 * @ClassName WelcomePresenter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/3 13:22
 * @Version 1.0
 */
public class WelcomePresenter extends BasePresenter<IWelcomeModel.IWelcomeView> {

    private WelcomeModel welcomeModel;

    public WelcomePresenter() {
        welcomeModel = new WelcomeModel();
    }

    private CountDownTimer timer = new CountDownTimer(1000 * 3, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            mRootView.showTimeTxt(millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            LogUtils.d("CountDownTimer onFinish");
            cancel();
            mRootView.showTimeTxt(0);
            mRootView.navigateToHome();
        }
    };

    public void startTime() {
        timer.start();
    }

    public void finishTime() {
        LogUtils.d("finishTime =" + (timer == null));
        timer.onFinish();
    }

    public void checkTokenLogin() {
        welcomeModel.checkLogin(new IWelcomeModel.WelcomeModelCheckLogin() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess() {
                mRootView.onSuccess();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (welcomeModel != null) {
            welcomeModel.clearDisposable();
            welcomeModel = null;
        }
    }
}
